//
//  HomeViewController.m
//  NewApp
//
//  Created by 健 吕 on 12-6-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "HomeViewController.h"
#import "ImageGalleryView.h"
#import "NAImage.h"
#import "ThumbnailGalleryView.h"
#import "SystemInlineViewController.h"
#import "ProductData.h"


@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10.f, 35.f, 300.f, 330.f)];
    //scrollView.backgroundColor = [UIColor purpleColor];
    scrollView.contentSize = CGSizeMake(300.f, 500.f);
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    
    galleryView = [[ImageGalleryView alloc] initWithFrame:CGRectMake(0.f, 0.f, 300.f, 225.f)];
    galleryView.delegate = self;
    galleryView.backgroundColor = [UIColor clearColor];
    [scrollView addSubview:galleryView];
    
    
    thumbGalleryView = [[ThumbnailGalleryView alloc] initWithFrame:CGRectMake(0.f, 245.f, 300.f, 270.f)];
    //thumbGalleryView.backgroundColor = [UIColor purpleColor];
    [scrollView addSubview:thumbGalleryView];
    
    [scrollView release];
    
    //Request gallery
    [self.engine homePageInfos];
}

- (void) dealloc {    
    [galleryView release];
    
    [super dealloc];
}

#pragma mark -
#pragma mark - Private methods

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *result = (NSDictionary *) object;
        [result retain];
        
        self.titleLabel.text = [result objectForKey:@"top_title"];
        
        NSArray *images = [[result objectForKey:@"data"] retain];
        NSArray *thumbnails = [[result objectForKey:@"data2"] retain];
        NSMutableArray *largeImages = [[NSMutableArray alloc] init];
        NSMutableArray *thumbnailImages = [[NSMutableArray alloc] init];
        
        
        for (NSDictionary *dic in images) {
            NAImage *image = [[NAImage alloc] initWithDictionary:dic];
            [largeImages addObject:image];
        }
        
        for (NSDictionary *dic in thumbnails) {
            NAImage *image = [[NAImage alloc] initWithDictionary:dic];
            [thumbnailImages addObject:image];
        }
        
        [galleryView setGalleryWithLargeImages:largeImages];
        [thumbGalleryView setGalleryWithLargeImages:thumbnailImages];
        
        dataArray = [[NSMutableArray alloc] initWithArray:thumbnailImages];
        
        //add click the thumnail event
        for (int i =0; i<[thumbGalleryView.imgsView count]; i++) {
            
            ((UIImageView *)[thumbGalleryView.imgsView objectAtIndex:i]).userInteractionEnabled=YES;
            UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickImage:)];
            singleTap.numberOfTouchesRequired = 1; 
            singleTap.numberOfTapsRequired = 1; 
            [((UIImageView *)[thumbGalleryView.imgsView objectAtIndex:i]) addGestureRecognizer:singleTap];
            
        }

        [thumbnailImages release];
        [largeImages release];
        [thumbnails release];
        [images release];
        [result release];
    }
}

- (BOOL) isInRect:(CGPoint)center fromPoint:(CGPoint)start toPoint:(CGPoint)end{
    
    if ((center.x < end.x && center.y < end.y)&&(center.x > start.x && center.y > start.y) ){
        return YES;
    }
    return NO;
}

- (void) onClickImage:(UITapGestureRecognizer *)sender{
    
    int n = 0;
    
    NSLog(@"Click point:x=%f,y=%f",sender.view.center.x,sender.view.center.y);
    
    while (n<[thumbGalleryView.imgsRect count]) {
        
        ImgRect *imgRect = (ImgRect *)[thumbGalleryView.imgsRect objectAtIndex:n];
        
        NSLog(@"Image range from x=%f,y=%f to x=%f,y=%f ",imgRect.start.x,imgRect.start.y,imgRect.end.x,imgRect.end.y);
        
        if ([self isInRect:sender.view.center fromPoint:imgRect.start toPoint:imgRect.end])
            break;
        else {
            n++;
        }
    }
    
	if (sender.numberOfTouchesRequired == 1 && n < [thumbGalleryView.imgsRect count]) {
        //to send request for the detail info.
        NSLog(@"You just click the iamge ID:%@",((ProductEntity *)[dataArray objectAtIndex:n]).ID);
        SystemInlineViewController *vctrl = [[SystemInlineViewController alloc] init];
        vctrl.ID = ((ProductEntity *)[dataArray objectAtIndex:n]).ID;
        [self.navigationController pushViewController:vctrl animated:YES];
        [vctrl release];
	}
}



@end
